package com.ultrasoft.farmasoft.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ultrasoft.farmasoft.domain.Typecomposition;
  
@Repository(value ="typeCompositionDao")
@Transactional(readOnly = false)
public class TypeCompositionDaoImpl implements TypeCompositionDao {

	private EntityManager em = null;	
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}
	
	
	@SuppressWarnings("unchecked")
	public List<Typecomposition> getTypecompositionList() {
		return em.createNamedQuery("Typecomposition.findAll").getResultList();
	}

 /*
	//si llegara e tener un active ---- añadir active en la bd
	@SuppressWarnings("unchecked")
	public List<Typecomposition> getTypecompositionList(boolean active) {
		Query  q = em.createNamedQuery("Typecomposition.findByActive");
    	q.setParameter("active",active);
    	return q.getResultList();	
	}
	
	
	 Query q1 = em.createQuery("SELECT c FROM Country c");
 
  TypedQuery<Country> q2 =
      em.createQuery("SELECT c FROM Country c", Country.class);
*/

	public void add(Typecomposition typeComposition) {
		em.persist(typeComposition);
	}

	public void remove(Typecomposition typeComposition) {
		try {
			em.remove(em.getReference(Typecomposition.class,typeComposition.getTypeCompositionId()));
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public void update(Typecomposition typeComposition) {
		em.merge(typeComposition);
	}

	public Typecomposition findTypecompositionByID(Integer idTypeComposition) {
		Typecomposition res=null;
		try {
	    	res=em.find(Typecomposition.class,idTypeComposition);
		} catch (Exception e) {}
    	return res;
	}
}
